#include "Mushroom.h"
#include "TerrainEx.h"


Mushroom::Mushroom(SceneManager* mscenemgr, TerrainEx* mterrain)
{
	
	m_pSceneMgr				= mscenemgr;
	m_pterrain				= mterrain;
	m_fWalkingSpeed			= 0.5f;
	m_fWalkingScale			= 1.0f;
	m_fjumpScale			= 1.0f;
	m_tjumping				= 0;
	m_bWalking				= false;
	m_fjumpVelocity			= JUMPHEIGHT;

}

int Mushroom::m_nCount = 0;

void Mushroom::createMushroom(Vector3& fromPos, SceneNode* MushroomTraceNode)
{
	m_pMushroomNode = 0;;
	m_pMushroomTraceNode = MushroomTraceNode;

	Ogre::String ver_index = Ogre::StringConverter::toString(m_nCount);
	m_pJumpNode		= m_pSceneMgr->getRootSceneNode()->createChildSceneNode("jumpNode" + ver_index);
	m_pJumpEntity	= m_pSceneMgr->createEntity("jumpEntity" + ver_index, SceneManager::PT_CUBE);
	m_pJumpNode->attachObject(m_pJumpEntity);
	m_pJumpNode->setScale(0.1, 0.1, 0.1);
	m_pJumpNode->setVisible(false);
	m_pJumpNode->_setDerivedPosition(fromPos);


	m_pMushroomEntity	= m_pSceneMgr->createEntity("mushroomEntity" + ver_index ,"penguin.mesh");
	m_pMushroomNode		= m_pJumpNode->createChildSceneNode("mushroomNode" + ver_index);
	m_pMushroomNode->attachObject(m_pMushroomEntity);


	m_pMushroomRangeEntity		= m_pSceneMgr->createEntity("m_pMushroomRangeEntity" + ver_index, SceneManager::PT_SPHERE);
	m_pMushroomRangeNode			= m_pMushroomNode->createChildSceneNode("m_pMushroomRangeNode" + ver_index);
	m_pMushroomRangeNode->scale(0.885, 0.885, 0.885);
	m_pMushroomRangeNode->attachObject(m_pMushroomRangeEntity);
	m_pMushroomRangeNode->setVisible(false);
	m_pMushroomRangeNode->showBoundingBox(false);	

	m_pMushroomCollisionEntity		= m_pSceneMgr->createEntity("m_pMushroomCollisionEntity" + ver_index, SceneManager::PT_CUBE);
	m_pMushroomCollisionNode		= m_pMushroomNode->createChildSceneNode("m_pMushroomCollisionNode" + ver_index);
	m_pMushroomCollisionNode->scale(0.3, 0.3, 0.3);
	m_pMushroomCollisionNode->attachObject(m_pMushroomCollisionEntity);
	m_pMushroomCollisionNode->setVisible(false);
	m_pMushroomCollisionNode->showBoundingBox(false);

	mp_animState =  m_pMushroomEntity->getAnimationState("amuse");
	mp_animState->setLoop(true);
	m_nCount++;

}

void Mushroom::updateMushroom(const Ogre::FrameEvent &evt)
{

	traceObject(evt);
	jumpObject(evt);
	updateAnimations(evt);
	fadeAnimations(evt);
}


void Mushroom::traceObject(const Ogre::FrameEvent &evt)
{
	// direction
	Ogre::Vector3 translatepos = m_pMushroomTraceNode->_getDerivedPosition() - m_pMushroomNode->_getDerivedPosition();	
	translatepos.normalise();
	Ogre::Vector3 src = m_pMushroomNode->_getDerivedOrientation() * Ogre::Vector3::UNIT_Z;
	Ogre::Quaternion quat = src.getRotationTo(translatepos*Vector3(1,0, 1));
	m_pMushroomNode->yaw(quat.getYaw(true));
	// collision

	AxisAlignedBox mushroomCollisionBox	=m_pMushroomRangeNode->_getWorldAABB();
	AxisAlignedBox trackCollisionBox	=m_pMushroomTraceNode->_getWorldAABB();

	if(mushroomCollisionBox.intersects(trackCollisionBox))
	{
		
		m_fWalkingScale = 0;
	
	}
	else
	{
		m_fWalkingScale	= m_fWalkingSpeed * evt.timeSinceLastFrame * MUSHROOMFRAMESCALE *0.60;
		m_tjumping = 1;
		m_bWalking = true;
		

	}

	// translate
	m_pMushroomNode->translate(translatepos *m_fWalkingScale);


	
}

void Mushroom::jumpObject(const Ogre::FrameEvent &evt)
{
	if (m_tjumping)
	{
		m_fjumpScale	= m_fjumpVelocity *evt.timeSinceLastFrame *0.0045 * MUSHROOMFRAMESCALE;
		m_pJumpNode->translate(Vector3(0, m_fjumpScale, 0));
		m_fjumpVelocity -=  JUMPHEIGHT *evt.timeSinceLastFrame *0.0045 * MUSHROOMFRAMESCALE;
	}
/*	
	if (m_pJumpNode->getPosition().y <= -0.85)
	{
		m_fjumpVelocity = JUMPHEIGHT;
		m_tjumping = 0;
		m_bWalking = false;
	}
	*/
	if (m_pMushroomNode->_getDerivedPosition().y <= checkGround().y)
	{
		m_fjumpVelocity = JUMPHEIGHT;
		m_tjumping = 0;
			m_bWalking = false;

	}

}

Vector3 Mushroom::checkGround()
{
	if (m_pterrain)
	{
		Ogre::Ray mouseRay(Vector3(m_pMushroomRangeNode->_getDerivedPosition().x, m_pMushroomRangeNode->_getDerivedPosition().y + 20, m_pMushroomRangeNode->_getDerivedPosition().z), Vector3::NEGATIVE_UNIT_Y);
		Ogre::TerrainGroup::RayResult&	result = m_pterrain->getTerrainGroup()->rayIntersects(mouseRay);

		Ogre::Vector3 chpos = Ogre::Vector3::ZERO;
		if (result.hit)
		{	
			chpos = result.position + Vector3(0, 2.65, 0);
			return chpos;
		}
	}else
	{
		return Ogre::Vector3::ZERO;
	}
	return Ogre::Vector3::ZERO;
}

void Mushroom::updateAnimations(const Ogre::FrameEvent &evt)
{
	if (m_bWalking == false)
	{
		mp_animState->setEnabled(true);
		mp_animState->addTime(evt.timeSinceLastFrame * MUSHROOMFRAMESCALE * 0.001);
	}else
	{
		mp_animState->setEnabled(false);
		mp_animState->setTimePosition(0);
		mp_animState->setWeight(0);
	}
}

void Mushroom::fadeAnimations(const Ogre::FrameEvent &evt)
{
	// slowly fade this animation in until it has full weight
	Real newWeight = mp_animState->getWeight();
	if (newWeight <= 0)
	{
		newWeight = mp_animState->getWeight() + evt.timeSinceLastFrame * MUSHROOMFRAMESCALE * 0.01;
		mp_animState->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
	}else if (newWeight >= 1)
	{
		newWeight = mp_animState->getWeight() - evt.timeSinceLastFrame * MUSHROOMFRAMESCALE * 0.01;
		mp_animState->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
	}


}